package com.my.download;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * Created by YJH on 2017/7/17 15:56.
 */
public class DownloadServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/x-msdownload");

        String fileName = "文件下载.pptx";
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

        OutputStream out = response.getOutputStream();
        String pptFileName = "D:\\CommonFile\\项目资料\\蒙牛\\WSP2.0 SDS用户手册\\WSP2.0预销直控不使用APP操作手册.pptx";

        InputStream in = new FileInputStream(pptFileName);

        byte[] buffer = new byte[1024];
        int len = 0;

        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }

        in.close();
    }
}
